package icu.groot.alan.service.impl;

import icu.groot.alan.dao.AnnounceDao;
import icu.groot.alan.dao.impl.AnnounceDaoImpl;
import icu.groot.alan.entity.Announce;
import icu.groot.alan.service.AnnounceService;
import icu.groot.alan.util.Alan;
import icu.groot.alan.util.Dao;
import icu.groot.alan.util.R;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class AnnounceServiceImpl implements AnnounceService {
    AnnounceDao announceDao = new AnnounceDaoImpl();
    @Override
    public List<Announce> getAllAnnounceTask() {
        Connection connection = Dao.getConnection();
        List<Announce> announces = new ArrayList<>();
        try {
            announces = announceDao.getAllAnnounceTask(connection);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        int announcesSize = announces.size() - 1;
        for (; announcesSize >= 0; announcesSize--) {
            Announce announce = announces.get(announcesSize);
            announce.setStartTimeStr(Alan.timestampToStr(announce.getStartTimeStamp(), Alan.FORMAT_LOCAL_DATE_TIME));
            announce.setEndTimeStr(Alan.timestampToStr(announce.getEndTimeStamp(), Alan.FORMAT_LOCAL_DATE_TIME));
        }
        Dao.closeResource(connection);
        return announces;
    }

    @Override
    public List<Announce> getNeedComplete(String userCode) {
        Connection connection = Dao.getConnection();

        List<Announce> announces = new ArrayList<>();
        try {
            List<Integer> alreadyReportAnnounceIds = announceDao.getAlreadyReportAnnounceIds(connection, System.currentTimeMillis() / 1000, Integer.parseInt(userCode));
            announces = announceDao.getNeedComplete(connection, System.currentTimeMillis() / 1000);
            announces = announces.stream().filter(item -> !alreadyReportAnnounceIds.contains(item.getId())).collect(Collectors.toList());
        } catch (SQLException e) {
            e.printStackTrace();
            return announces;
        }
        int announcesSize = announces.size() - 1;
        for (; announcesSize >= 0; announcesSize--) {
            Announce announce = announces.get(announcesSize);
            announce.setStartTimeStr(Alan.timestampToStr(announce.getStartTimeStamp(), Alan.FORMAT_LOCAL_DATE_TIME));
            announce.setEndTimeStr(Alan.timestampToStr(announce.getEndTimeStamp(), Alan.FORMAT_LOCAL_DATE_TIME));
        }
        Dao.closeResource(connection);
        return announces;
    }

    @Override
    public R announceTask(String taskCode, String endTime, String code) {
        Connection connection = Dao.getConnection();
        try {
            int endTimeStamp = (int)Alan.localDateTimeStrToTimestamp(endTime.replace("T", " ") + ":00", Alan.FORMAT_LOCAL_DATE_TIME);
            int updateRow = announceDao.accounceTask(connection, Integer.parseInt(taskCode), (int)(System.currentTimeMillis() / 1000), endTimeStamp);
            connection.commit();
            if (updateRow <= 0) throw new RuntimeException("插入数据小于等于0");
        } catch (SQLException e) {
            try {
                connection.rollback();
            } catch (SQLException ex) {
                e.printStackTrace();
            }
            return R.error("参数异常");
        }
        Dao.closeResource(connection);
        return R.ok("发布成功");
    }

}
